package router

import (
	"gf-app/app/api/hello"
	"gf-app/app/lib/error_msg"
	"gf-app/app/lib/resp"
	"gf-app/app/middleware"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/glog"
)

// 统一路由注册.
func init() {
	g.Server().BindHandler("/", hello.Handler)

	//对象注册
	helloObj := new(hello.Test)
	g.Server().BindObject("/test", helloObj)
	//路由内置变量
	g.Server().BindObject("/{.struct}-{.method}", helloObj)

	//中间件
	s := g.Server()
	//请求日志中间件
	s.BindMiddlewareDefault(middleware.LogMiddleware)

	s.Group("/api/v1", func(group *ghttp.RouterGroup) {
		group.Middleware(middleware.AuthMiddleware, middleware.MiddlewareCors)
		group.GET("/test", helloObj.Index)
	})

	//鉴权例外处理
	s.Group("/admin", func(group *ghttp.RouterGroup) {
		group.ALL("/login", func(r *ghttp.Request) {
			r.Response.Writeln("login")
		})
		group.Group("/", func(group *ghttp.RouterGroup) {
			group.Middleware(middleware.MiddlewareCors, middleware.AuthMiddleware, middleware.ErrorHandlerMiddleware)
			group.ALL("dashboard", func(r *ghttp.Request) {
				r.Response.Writeln("dashboard")
			})
			group.ALL("test", func(r *ghttp.Request) {
				r.Response.Writeln("test")
			})
			group.ALL("test/errorhandler", func(r *ghttp.Request) {
				panic("db error: sql is xxxxxxx")
			})
			//测试请求参数
			group.ALL("testParams", hello.TestParams)
			//测试cookie
			group.ALL("testCookie", hello.TestCookie)
			//session测试
			group.Group("/session", func(group *ghttp.RouterGroup) {
				group.ALL("set", hello.SetSession)
				group.ALL("get", hello.GetSession)
				group.ALL("del", hello.DelSession)
			})
			//测试发起请求
			group.ALL("testRequest", hello.TestRequest)
		})
		group.Group("/test", func(group *ghttp.RouterGroup) {
			group.GET("foo", func(r *ghttp.Request) {
				r.Response.Write(r.URL)
			})
		})
	})
	//websocket服务器
	s.BindHandler("/ws", hello.WebsocketHandle)
	//绑定404响应回调函数
	s.BindStatusHandler(404, func(r *ghttp.Request) {
		r.Response.WriteJsonExit(g.Map{
			"code": 0,
			"info": "请求的路由不存在",
		})
	})
	//绑定500响应回调函数
	s.BindStatusHandlerByMap(map[int]ghttp.HandlerFunc{
		500: func(r *ghttp.Request) {
			glog.Error(r.Response.BufferString())
			r.Response.ClearBuffer()
			resp.Error(r, error_msg.RUNTIME_ERROR)
		},
	})
	//获取配置数据
	s.BindHandler("/GetConfig", hello.GetConfig)
	//测试错误
	s.BindHandler("/TestError", hello.TestError)
	//测试日志
	s.BindHandler("/test_log", hello.TestLog)
	//测试日志trace
	s.BindHandler("/TestTrace", hello.TestTrace)
	//测试数据校验
	s.BindHandler("/DataCheck", hello.DataCheck)
	//测试数据库
	s.BindHandler("/TestDb", hello.TestDb)
	//测试数据插入
	s.BindHandler("/TestInsert", hello.TestInsert)
	//测试数据更新
	s.BindHandler("/TestUpdate", hello.TestUpdate)
	//测试数据删除
	s.BindHandler("/TestDel", hello.TestDel)
	//测试数据查询
	s.BindHandler("/TestSearch", hello.TestSearch)
	//测试数据库缓存
	s.BindHandler("/TestDbCache", hello.TestDbCache)
	s.Group("/db", func(group *ghttp.RouterGroup) {
		group.GET("/TestFuncInsert", hello.TestFuncInsert)
		group.GET("/TestFuncSearch", hello.TestFuncSearch)
		group.GET("/TestFuncSave", hello.TestFuncSave)
		group.GET("/TestFuncBatchInsert", hello.TestFuncBatchInsert)
	})
	s.BindHandler("/Testgmap", hello.Testgmap)
	s.BindHandler("/TestGmapIterator", hello.TestGmapIterator)
	s.BindHandler("/Testgval", hello.Testgval)
	s.BindHandler("/TestGtype", hello.TestGtype)
	s.BindHandler("/Testgqueue", hello.Testgqueue)
	s.BindHandler("/TestQueueSelect", hello.TestQueueSelect)
	s.BindHandler("/Testgstr", hello.Testgstr)
	s.BindHandler("/Testgjson", hello.Testgjson)
	s.BindHandler("/Testgjson1", hello.Testgjson1)
	s.BindHandler("/Testgparser", hello.Testgparser)
	s.BindHandler("/Testgparser1", hello.Testgparser1)
	s.BindHandler("/Testparsesr2", hello.Testparsesr2)
	s.BindHandler("/Testparser3", hello.Testparser3)
	s.BindHandler("/Testparser4", hello.Testparser4)
	s.BindHandler("/Testparser5", hello.Testparser5)
	s.BindHandler("GET:/Testparser6", hello.Testparser6)
	s.BindHandler("/Testparser7", hello.Testparser7)
	s.BindHandler("/Test_gbase64", hello.Test_gbase64)
	s.BindHandler("/Test_hash_hmac", hello.Test_hash_hmac)
	s.BindHandler("/Test_gtime", hello.Test_gtime)
	s.BindHandler("/Test_gmutex", hello.Test_gmutex)
	s.BindHandler("/Test_gproc", hello.Test_gproc)
	s.BindHandler("/Test_gproc1", hello.Test_gproc1)
	s.BindHandler("/Test_grpool", hello.Test_grpool)
	s.BindHandler("/Test_gmd5", hello.Test_gmd5)
	s.BindHandler("/Test_gconv", hello.Test_gconv)
	s.BindHandler("/Test_grand", hello.Test_grand)
	s.BindHandler("/Test_guuid", hello.Test_guuid)
	//测试分页
	s.BindHandler("/Test_page", hello.Test_page)
	s.BindHandler("/Test_getpage", hello.Test_getpage)
	s.BindHandler("/MakeNewPasswd", hello.MakeNewPasswd)

	//s.EnableAdmin()
	//s.EnablePProf()
}
